/* ==============================================================================
*
*   Copyright (c) 2010, Pierre Favier
*   All rights reserved.
*
*   NTasking is free software: you can redistribute it and/or modify it
*   under the terms of the GNU General Public License as published by the
*   Free Software Foundation.
*
*   Redistribution and use in source and binary forms, with or without
*   modification, are permitted provided that the following conditions
*   are met:
*
*   -   Redistributions of source code must retain the above copyright notice,
*       this list of conditions and the following disclaimer.
*
*   -   Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials
*       provided with the distribution.
*
*   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
*   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
*   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
*   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
*   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
*   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
*   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
*   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*   SUCH DAMAGE.
*
* ============================================================================== */

using System;
using NUnit.Framework;
using Rhino.Mocks;
using Rhino.Mocks.Constraints;

namespace NTasking.ValidationTests.Other.SyntaxErrors
{

    [TestFixture]
    public class SynchronizableAttribute : ValidationTest
    {

        internal interface IConflictingAttribute
        {
            [Entry]
            Action AnEntry { get; }
        }


        internal class ConflictingAttributeProtected : Protected, IConflictingAttribute
        {

            [ReadWriteOperation]
            public Action AnEntry { get; private set; }


            public ConflictingAttributeProtected()
            {
                ReadWriteOperation(AnEntry, () => { });
            }
        }


        [Test]
        public void ConflictingAttribute()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            ConflictingAttributeProtected prot = null;

            Ada.Declare(() =>
            {
                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();

                try
                {
                    prot = new ConflictingAttributeProtected();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ConflictingAttributeError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
                // Setup text expectations

            },
            Begin: () =>
            {
            });
        }


        internal class NoSetterAttributeProtected : Protected
        {

            [ReadWriteOperation]
            public Action AnEntry { get { return null; } }


            public NoSetterAttributeProtected()
            {
                ReadWriteOperation(AnEntry, () => { });
            }
        }


        [Test]
        public void NoSetter()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            NoSetterAttributeProtected prot = null;

            Ada.Declare(() =>
            {
                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();

                try
                {
                    prot = new NoSetterAttributeProtected();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.NoPropertySetterError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
                // Setup text expectations

            },
            Begin: () =>
            {
            });
        }


        internal class SetterNotPrivateProtected : Protected
        {

            [ReadWriteOperation]
            public Action AnEntry { get; set; }


            public SetterNotPrivateProtected()
            {
                ReadWriteOperation(AnEntry, () => { });
            }
        }


        [Test]
        public void SetterIsNotPrivate()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            SetterNotPrivateProtected prot = null;

            Ada.Declare(() =>
            {
                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();

                try
                {
                    prot = new SetterNotPrivateProtected();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.PropertySetterIsNotPrivateError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
                // Setup text expectations

            },
            Begin: () =>
            {
            });
        }


        internal class TaskWithReadWriteOperation : Task
        {
            [ReadWriteOperation]
            internal Func<bool> Entry { get; private set; }


            protected override void Body()
            {
            }
        }


        [Test]
        public void ReadWriteOperationNotAllowedInTask()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            TaskWithReadWriteOperation task = null;

            Ada.Declare(() =>
            {
                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();

                try
                {
                    task = new TaskWithReadWriteOperation();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ProtectedOperationNotAllowedInTaskError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
                // Setup text expectations

            },
            Begin: () =>
            {
            });
        }


        internal class TaskWithReadOnlyOperation : Task
        {
            [ReadOnlyOperation]
            internal Func<bool> Entry { get; private set; }


            protected override void Body()
            {
            }
        }


        [Test]
        public void ReadOnlyOperationNotAllowedInTask()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            TaskWithReadOnlyOperation task = null;

            Ada.Declare(() =>
            {
                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();

                try
                {
                    task = new TaskWithReadOnlyOperation();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ProtectedOperationNotAllowedInTaskError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
                // Setup text expectations

            },
            Begin: () =>
            {
            });
        }


        internal class ProtectedWithReadWriteFamily : Protected
        {

            [ReadWriteOperation]
            public FuncFamily<int, bool> AnEntry { get; private set; }


            public ProtectedWithReadWriteFamily()
            {
            }
        }


        [Test]
        public void ReadWriteFamilyNotAllowed()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            ProtectedWithReadWriteFamily prot = null;

            Ada.Declare(() =>
            {
                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();

                try
                {
                    prot = new ProtectedWithReadWriteFamily();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ProtectedOperationNotAllowedForFamilyError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
                // Setup text expectations

            },
            Begin: () =>
            {
            });
        }


        internal class ProtectedWithReadOnlyFamily : Protected
        {

            [ReadOnlyOperation]
            public FuncFamily<int, bool> AnEntry { get; private set; }


            public ProtectedWithReadOnlyFamily()
            {
            }
        }


        [Test]
        public void ReadOnlyFamilyNotAllowed()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            ProtectedWithReadOnlyFamily prot = null;

            Ada.Declare(() =>
            {
                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();

                try
                {
                    prot = new ProtectedWithReadOnlyFamily();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ProtectedOperationNotAllowedForFamilyError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
                // Setup text expectations

            },
            Begin: () =>
            {
            });
        }


        internal class ProtectedWithReadOnlyAction : Protected
        {

            [ReadOnlyOperation]
            public Action<int, bool> AnEntry { get; private set; }


            public ProtectedWithReadOnlyAction()
            {
            }

        }


        [Test]
        public void ReadOnlyActionNotAllowed()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            ProtectedWithReadOnlyAction prot = null;

            Ada.Declare(() =>
            {
                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();

                try
                {
                    prot = new ProtectedWithReadOnlyAction();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.InvalidReadOnlyMethodTypeError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
                // Setup text expectations

            },
            Begin: () =>
            {
            });
        }


        internal class ProtectedWithUnsupportedType : Protected
        {

            [Entry]
            public EventHandler<EventArgs> AnEntry { get; private set; }


            public ProtectedWithUnsupportedType()
            {
            }

        }


        internal class TaskWithUnsupportedType : Task
        {

            [Entry]
            public EventHandler<EventArgs> AnEntry { get; private set; }


            public TaskWithUnsupportedType()
            {
            }

            protected override void Body()
            {
            }
        }


        [Test]
        public void UnsupportedMethodType()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            ProtectedWithUnsupportedType prot = null;
            TaskWithUnsupportedType task = null;

            Ada.Declare(() =>
            {
                // Setup text expectations
                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();

                try
                {
                    prot = new ProtectedWithUnsupportedType();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.UnsupportedSynchronizedMethodTypeError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    task = new TaskWithUnsupportedType();

                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.UnsupportedSynchronizedMethodTypeError)
                        // Wrong error detected
                        result.TestFailed();
                }
                catch (Exception ex)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            },
            Begin: () =>
            {
            });
        }

    }

}
